package com.petdog.module.community.controller;

import com.petdog.common.dto.ApiResponse;
import com.petdog.common.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 媒体文件上传控制器
 */
@RestController
@RequestMapping("/api/media")
public class MediaController {
    
    @Autowired
    private FileUploadUtil fileUploadUtil;
    
    /**
     * 上传单张图片
     */
    @PostMapping("/upload/image")
    public ApiResponse<String> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            String imageUrl = fileUploadUtil.uploadImage(file);
            return ApiResponse.success(imageUrl);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(e.getMessage());
        } catch (IOException e) {
            return ApiResponse.error("媒体上传失败，已为您保留输入内容，请重试上传");
        }
    }
    
    /**
     * 上传多张图片
     */
    @PostMapping("/upload/images")
    public ApiResponse<List<String>> uploadImages(@RequestParam("files") MultipartFile[] files) {
        try {
            List<String> imageUrls = fileUploadUtil.uploadImages(files);
            return ApiResponse.success(imageUrls);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(e.getMessage());
        } catch (IOException e) {
            return ApiResponse.error("媒体上传失败，已为您保留输入内容，请重试上传");
        }
    }
    
    /**
     * 上传视频
     */
    @PostMapping("/upload/video")
    public ApiResponse<String> uploadVideo(@RequestParam("file") MultipartFile file) {
        try {
            String videoUrl = fileUploadUtil.uploadVideo(file);
            return ApiResponse.success(videoUrl);
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(e.getMessage());
        } catch (IOException e) {
            return ApiResponse.error("媒体上传失败，已为您保留输入内容，请重试上传");
        }
    }
}